const express = require("express");
const router = express.Router();
const cors = require("cors");
const util = require("util");
const axios = require("axios").default;

router.use(cors());

router.post("/surviv.findgame/hk", async (req, res) => {
  let args_body = JSON.parse(JSON.stringify(req.body));
  if (args_body.zones && util.isArray(args_body.zones)) {
    args_body.zones = ["hkg"];
  }
  axios.post("https://surviv.io/api/find_game", args_body, {
    headers: {
      'Content-Type': 'application/json; charset=UTF-8',
      'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 OPR/57.0.3098.106',
      'X-Requested-With': 'XMLHttpRequest'
    }
  }).then(axresp => {
    for (let h in axresp.headers) {
      res.setHeader(h, axresp.headers[h]);
    }
    let data = axresp.data;
    data['_debug'] = {
      "proxy": "keisatsu.s.cloudno.de",
      "desc": "i love hongkong server",
      "time": Date.now(),
      "origin_args": req.body
    };
    res.status(axresp.status).send(data);
  }).catch(axerr => {
    res.status(500).send({
      ok: false,
      msg: "get game failed"
    })
  })

})




module.exports = router;